package model

import (
	"gorm.io/gorm"
	"time"
)

type InsulinTherapy struct {
	gorm.Model
	UserId         uint      `json:"user_id"`
	SaveTime       time.Time `json:"save_time"`
	Tdi            float32   `json:"tdi"`
	BasalTdi       float32   `json:"basal_tdi"`
	BolusTdi       float32   `json:"bolus_tdi"`
	BasalInject    float32   `json:"basal_inject"`
	Pump_0_3       float32   `json:"pump_0_3"`
	Pump_3_8       float32   `json:"pump_3_8"`
	Pump_8_12      float32   `json:"pump_8_12"`
	Pump_12_16     float32   `json:"pump_12_16"`
	Pump_16_20     float32   `json:"pump_16_20"`
	Pump_20_24     float32   `json:"pump_20_24"`
	BreakfastBolus float32   `json:"breakfast_bolus"`
	LunchBolus     float32   `json:"lunch_bolus"`
	DinnerBolus    float32   `json:"dinner_bolus"`
	BedtimeBasal   float32   `json:"bedtime_basal"`
}

func (t *InsulinTherapy) Create(db *gorm.DB) error {
	return db.Create(t).Error
}

func (t *InsulinTherapy) GetByUserId(db *gorm.DB) error {
	return db.Where(Eq("user_id", t.UserId)).First(t).Error
}

func (t *InsulinTherapy) Delete(db *gorm.DB) error {
	return db.Delete(t).Error
}

func (t *InsulinTherapy) Save(db *gorm.DB) error {
	return db.Save(t).Error
}

func (t *InsulinTherapy) GetBefore(db *gorm.DB, before Time) error {
	return db.Where(Eq("user_id", t.UserId)).Where(LtEq("save_time", before.Time)).Order("save_time desc").Find(t).Error
}
